import { request } from '@/utils/request'
import type { User, CodeType, UserInfo, Patient, PatientList } from '@/types/user'

export const loginByPassword = (data: { mobile: string; password: string }) => {
  /**
   * 如果想要能通过 res.data 得到我们想要的返回值类型，那么我们的泛型就加在第二个泛型参数上
   */
  /** 
  return instance<null, { code: number; message: string; data: User }>({
    url: 'login/password',
    method: 'POST',
    data
  })
  */
  /**
   * 未封装前的代码
  return instance<null, ApiRes<User>>({
    url: 'login/password',
    method: 'POST',
    data
  })
   */

  /**
   * request函数，它接收一个泛型：res.data返回数据的类型
   * 参数1：地址
   * 参数2：请求方式
   * 参数3：数据
   */
  return request<User>('login/password', 'POST', data)
}

// 获取登录时候的验证码
export const getCode = (mobile: string, type: CodeType = 'login') =>
  request<{ code: string }>('/code', 'GET', {
    mobile,
    type
  })

// 验证码登录
export const codeLogin = (data: { mobile: string; code: string }) => {
  return request<User>('/login', 'POST', data)
}

// 获取用户信息
export const getUserInfo = () => {
  return request<UserInfo>('/patient/myUser')
}

// 获取患者列表
export const getPatientList = () => {
  return request<PatientList>('/patient/mylist')
}

// 添加患者
export const addPatient = (data: Patient) => {
  return request('/patient/add', 'POST', data)
}

// 修改患者
export const updatePatient = (data: Patient) => {
  return request('/patient/update', 'PUT', data)
}

// 删除患者
export const deletePatient = (id: string) => {
  return request(`patient/del/${id}`, 'DELETE')
}

// 查询患者信息
export const getPatientInfo = (id: string) => {
  return request<Patient>(`/patient/info/${id}`)
}

// qq三方登录
export const qqLogin = (openId: string, source = 'qq') => {
  return request<User>('/login/thirdparty', 'POST', { openId, source })
}

// 手机号绑定
export const bindMobile = (data: { mobile: string; code: string; openId: string }) => {
  return request<User>('/login/binding', 'POST', data)
}
